package LeetCode;

import java.util.ArrayList;
import java.util.List;
import java.util.Stack;

public class LC_094_BinaryTreeInorderTraversal {
    public static void main(String[] args) {

    }

    static class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }

    static class Solution {
        List<Integer> list = new ArrayList<>();

        public List<Integer> inorderTraversal(TreeNode root) {
            inOrder(root);
            return list;
        }

        private void inOrder(TreeNode node) {
            if (node == null) return;
            if (node.left != null) inOrder(node.left);
            list.add(node.val);
            if (node.right != null) inOrder(node.right);
        }
    }

    static class Solution_2 {
        List<Integer> list = new ArrayList<>();

        public List<Integer> inorderTraversal2(TreeNode root) {
            if (root == null) return list;
            Stack<TreeNode> stack = new Stack<>();
            TreeNode tempNode = root;
            stack.push(tempNode);
            while (!stack.isEmpty()) {
                while ((tempNode = stack.peek()) != null) {
                    tempNode = tempNode.left;
                    stack.push(tempNode);
                }
                stack.pop();
                if (!stack.isEmpty()) {
                    tempNode = stack.pop();
                    list.add(tempNode.val);
                    stack.push(tempNode.right);
                }
            }
            return list;
        }
    }
}
